package com.hqx.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hqx.common.constant.ProductConstant;
import com.hqx.gulimall.product.entity.ProductAttrValueEntity;
import com.hqx.gulimall.product.service.ProductAttrValueService;
import com.hqx.gulimall.product.vo.AttrRespVo;
import com.hqx.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hqx.gulimall.product.entity.AttrEntity;
import com.hqx.gulimall.product.service.AttrService;
import com.hqx.common.utils.PageUtils;
import com.hqx.common.utils.R;



/**
 * 
 *
 * @author hqx
 * @email 2322305634@qq.com
 * @date 2024-03-14 10:44:01
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     * attrtype 0：销售属性 1：基础属性(规格参数)
     */
    @RequestMapping("/{attrtype}/list/{catId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("attrtype") String attrtype, @PathVariable("catId") Long catId){
        int attrType;
        if(ProductConstant.AttrEnum.ATTR_TYPE_BASE.getDesc().equals(attrtype)){
            attrType = ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode();
        }else{
            attrType = ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode();
        }
        if(catId == 0)
            return R.ok().put("page", attrService.queryPage(params,attrType));
        return R.ok().put("page", attrService.queryPage(params,attrType,catId));
    }

    @RequestMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
       List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.getBaseAttrListForSpu(spuId);
        return R.ok().put("data",productAttrValueEntities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){
		attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    @RequestMapping("/update/{spuId}")
    public R updateBaseAttrForSpu(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> productAttrValueEntities){
        productAttrValueService.updateBaseAttrForSpu(spuId,productAttrValueEntities);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
